/***************************************************************************
 *   Copyright (C) 2007 by Luca Bellonda                                   *
 *   lbell @ tsc4.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

package lb.prove;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.Layout.Alignment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class RSSAdapter extends BaseAdapter {

    private Context mContext;
	List<RssItem> items = new ArrayList<RssItem>();
    
	public RSSAdapter(Context context, List<RssItem> newItems)
    {
        mContext = context;
        setRssItems(newItems);
    }
	int calcDays(Date da, final int nDaysDiff)
	{
		return (da.getDay()+nDaysDiff)+(da.getMonth()*100)+(da.getYear()*1000);
	}
	void addFakeItem( List<RssItem> theItems, String title)
	{
		RssItem ni = new RssItem();
		ni.isDate = true;
		ni.title = title;
		ni.guid = null;
		theItems.add(ni);
	}

	public void setRssItems(List<RssItem> newItems)
	{
		String future = mContext.getText(R.string.future).toString();
		String today = mContext.getText(R.string.today).toString();
		String yesterday = mContext.getText(R.string.yesterday).toString();
		String older = mContext.getText(R.string.later).toString();
		boolean isFuture = true;
		boolean isToday = true;
		boolean isYesterday = true;
		boolean isOlder = true;
		Date now = new Date();
		int nToday = calcDays(now, 0);
		int nYesterday = calcDays(now, -1);
		
		items = new ArrayList<RssItem>();
		if(null==newItems)
			return ;
		if(0==newItems.size())
			return ;
		for( RssItem item : newItems)
		{
			int nDays = calcDays(item.date,0);
			if(isFuture && (nDays>nToday))
			{
				isFuture = false;
				addFakeItem( items, future );
			} else if(isToday && (nDays==nToday))
			{
				isToday = false;
				addFakeItem( items, today );
			} else if(isYesterday && (nDays==nYesterday))
			{
				isYesterday = false;
				addFakeItem( items, yesterday );
			} else if(isOlder && (nDays<nYesterday))
			{
				isOlder = false;
				addFakeItem( items, older );
			}
			items.add(item);
		}
	}
	
	RssItem getRssItem(int position)
	{
		if( (position>=0) && (position >= items.size() ))
	        return null;
		return items.get(position);
	}
	
    public int getCount() {
        return items.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        RssItem item = items.get(position);
        if(item.isDate)
        {
        	TextView tv;
        	if ((convertView == null)||!(convertView instanceof TextView))
        	{
        		tv = new TextView(mContext);
                tv.setAlignment(Alignment.ALIGN_CENTER);
        		tv.setBackgroundColor(0xFF8000);
        	} else
        	{
        		tv = (TextView)convertView;
        	}
        	tv.setText(item.title);
        	return tv;
        }
        RssItemView iv;
        if ((convertView == null)||!(convertView instanceof RssItemView))
        {
            iv = new RssItemView(mContext, item);
        } else {
            iv = (RssItemView)convertView;
            iv.setRssItem(item);
        }
        return iv;
    }
}
